import { Module } from '@nestjs/common'
import { JwtModule } from '@nestjs/jwt'
import { AppController } from './app.controller'
import { AppService } from './app.service'
import { ConfigModule } from '@nestjs/config'
import { QuestionModule } from './question/question.module'
import { MongooseModule } from '@nestjs/mongoose'
import { UsersModule } from './users/users.module'
import { TokenGuard } from './guards/TokenGuard'
import { APP_FILTER, APP_GUARD } from '@nestjs/core'
import { AllExceptionsFilter } from './exception/AllExceptionsFilter'
import { ComponentsModule } from './components/components.module'
import { AnswerModule } from './answer/answer.module';

@Module({
  imports: [
    // 配置模块，用于读取配置文件
    ConfigModule.forRoot({
      isGlobal: true // 全局可用
    }),
    // mongodb模块
    MongooseModule.forRoot(`${process.env.MONGO_URL}`),
    // jwt模块
    JwtModule.register({
      global: true, // 全局可用
      secret: process.env.SECRET,
      signOptions: { expiresIn: '72h' } // token 过期时间
    }),
    // 自定义的Model模块
    QuestionModule,
    UsersModule,
    ComponentsModule,
    AnswerModule
  ],
  controllers: [AppController],
  providers: [
    AppService,
    // 添加全局守卫
    {
      provide: APP_GUARD,
      useClass: TokenGuard
    },
    // 全局异常处理
    {
      provide: APP_FILTER,
      useClass: AllExceptionsFilter
    }
  ]
})
export class AppModule {}
